//
//  CourseDetailController.m
//  TrainTree
//
//  Created by wanghui on 2017/9/12.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "CourseDetailController.h"
#import "CourseDetailTopCell.h"
#import "CourseIntroduceCell.h"
#import "DetailVideoCell.h"
#import "CourseAudioVideoController.h"
#import "CourseRequestManager.h"
#import "PointModel.h"
@interface CourseDetailController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL _fold; // 课程介绍展开折叠状态
    CourseDetailModel *_detailModel;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**数据数组*/
@property(nonatomic,strong)NSMutableArray *dataArray;
/**课程介绍行数*/
@property(nonatomic,assign)NSInteger lineNumber;

@end

@implementation CourseDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationView.hidden = YES;

    [self createTableView];
    [self requestDataWithLessonId];
    self.lineNumber = 4;
}

#pragma mark - 根据课程Id获取课程详情
- (void)requestDataWithLessonId
{
    WeakSelf(ws);
    [CourseRequestManager requestLessonDetailWithLessonId:self.lessonId reponseBlock:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if ([JSONData[@"c"] intValue] == 0) {
         _detailModel = [CourseDetailModel mj_objectWithKeyValues: JSONData[@"d"]];
            [ws.tableView reloadData];
        }
    }];
}

#pragma - mark tableView相关
- (void)createTableView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseIntroduceCell" bundle:nil] forCellReuseIdentifier:@"CourseIntroduceCell"];

    [self.tableView registerNib:[UINib nibWithNibName:@"CourseDetailTopCell" bundle:nil] forCellReuseIdentifier:@"CourseDetailTopCell"];

    [self.tableView registerNib:[UINib nibWithNibName:@"DetailVideoCell" bundle:nil] forCellReuseIdentifier:@"DetailVideoCell"];

     
}
- (NSMutableArray *)dataArray
{
    return _dataArray ?: (_dataArray = [NSMutableArray array]);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return _detailModel.points.count;
    } else {
        return 1;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        return 8;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 8)];
        view.backgroundColor = [UIColor whiteColor];
        return view ;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         return [tableView fd_heightForCellWithIdentifier:@"CourseDetailTopCell" configuration:^(id cell) {

         }];
    } else if(indexPath.section == 1) {
        return [tableView fd_heightForCellWithIdentifier:@"CourseIntroduceCell" configuration:^(id cell) {
            if(_detailModel) {
                CourseIntroduceCell *cc = (CourseIntroduceCell *)cell;
                [cc loadStr:_detailModel.lessonDesc Num:self.lineNumber];
            }


        }];
    } else {
         return [tableView fd_heightForCellWithIdentifier:@"DetailVideoCell" configuration:^(id cell) {

         }];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CourseDetailTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseDetailTopCell"];
        [cell.backBtn addTarget:self action:@selector(backToLast) forControlEvents:UIControlEventTouchUpInside];
        if (_detailModel) {
            [cell configDataWithStatus:_detailModel.joinStatus classify:[NSString stringWithFormat:@"%@/%@",_detailModel.themeName,_detailModel.moduleName] detailTitle:_detailModel.lessonName imgUrl:_detailModel.lessonCover lessonId:_detailModel.lessonId studyType:_detailModel.studyType];

        }
        return cell;
        
    } else if (indexPath.section == 1) {
        CourseIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseIntroduceCell"];

        [cell.checkMoreBtn addTarget:self action:@selector(changeCellHeight:) forControlEvents:UIControlEventTouchUpInside];
        [cell loadStr:_detailModel.lessonDesc Num:self.lineNumber];



        if (_detailModel.lessonDesc.length < 120 &&  self.lineNumber <= 4) {
            cell.checkMoreBtn.hidden = YES;
        }else{
            cell.checkMoreBtn.hidden = NO;
        }

        return cell;
    } else {
        DetailVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailVideoCell"];
            // 只有一个，左侧线不显示
            if (_detailModel.points.count == 1) {
                [cell configWithOnlyOne];
            }else{// 有多条数据
                if (indexPath.row == 0) {// 第一个隐藏上边竖线
                    [cell configWithFirstModel:_detailModel.points[indexPath.row]];
                }else if (indexPath.row == _detailModel.points.count - 1) {
                        // 最后一个隐藏下边的竖线
                    [cell configWithLastModel:_detailModel.points[indexPath.row]];
                }else {
                    [cell configWithModel:_detailModel.points[indexPath.row]];
                }
            }

        return cell;
        }
}
#pragma  mark - 播放视频、音频
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section >= 2) {
        CourseAudioVideoController *AVdieo = [[CourseAudioVideoController alloc]init];
        if (_detailModel) {
            PointModel *model = _detailModel.points[indexPath.row];
            AVdieo.pointId = model.pointId;
        }
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:AVdieo animated:YES];
    }

}
 
#pragma mark - 返回上个页面
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 打开折叠课程介绍
- (void)changeCellHeight:(UIButton *)btn
{
    if (_fold) {
        _fold = NO;
        self.lineNumber = 4;
        [btn setTitle:@"查看更多" forState:UIControlStateNormal];
    }else{
        _fold = YES;
        self.lineNumber = 0;
        [btn setTitle:@"收起" forState:UIControlStateNormal];
    }
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
